package cn.doctorpda.conference.update;

import android.util.Base64;
import android.util.Log;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 加密解密
 * Created by DELL on 2015/8/28.
 */
public class BaiyyyAESUtil {

    /**
     * 加密
     */
    @SuppressWarnings("restriction")
    public static String encrypt(String data, String seed) {
        String result = null;
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(seed.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(seed.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            //result = new BASE64Encoder().encode(encrypted);
            result = Base64.encodeToString(encrypted, Base64.DEFAULT);
            //result = URLEncoder.encode(result, "UTF-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 解密
     */
    @SuppressWarnings("restriction")
    //doctorpda8888888
    public static String decrypt(String data, String seed) {
        String result = null;
        try {
            //byte[] encrypted1 = new sun.misc.BASE64Decoder().decodeBuffer(data);
            byte[] encrypted1 = Base64.decode(data, Base64.DEFAULT);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(seed.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(seed.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            result = originalString.trim();
        } catch (Exception e) {
            Log.e("AES解密异常", "加密字符串" + data, e);
            return data;
        }
        return result;
    }

    public static String decrypt(String data, String seed, String failString) {
        String result = null;
        try {
            //byte[] encrypted1 = new sun.misc.BASE64Decoder().decodeBuffer(data);
            byte[] encrypted1 = Base64.decode(data, Base64.DEFAULT);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(seed.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(seed.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            result = originalString.trim();
        } catch (Exception e) {
            Log.e("AES解密异常", "加密字符串" + data, e);
            return failString;
        }
        return result;
    }
}
